package com.aaa.tmr.util;

import com.aaa.tmr.entity.User;
import com.aaa.tmr.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;

/**
 * @author ：Marko
 * @date ：Created in 2021/3/18 22:09
 * @description ：
 * @modified By：
 * @version:
 */
public class MyCustomRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String) authenticationToken.getPrincipal();
        User user = userService.queryByUserName(userName);
        if(user==null){
            throw new AccountException();
        }
        String password = user.getPassword();
        String salt = user.getSalt();
        return new SimpleAuthenticationInfo(user,password, ByteSource.Util.bytes(salt),getName());
    }
}
